﻿using System;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace MyTcpClient
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient client = new TcpClient();
            try
            {
                client.Connect("127.0.0.1", 5000);      // 与服务器连接
                byte[] buffer = new byte[1024];
                while (true)
                {
                    var stream = client.GetStream();
                    for (int i = 0; i < 10; i++)
                    {
                        stream.Write(Encoding.UTF8.GetBytes("测试连接" + i));
                        Thread.Sleep(1000);
                        stream.Read(buffer,0, buffer.Length);
                        string s = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                        Console.WriteLine("服务器返回: "+s);
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
            client.Close();
            Console.WriteLine("测试完毕，按任意键继续");
            Console.ReadKey();
        }
    }
}
